package com.zjsystem.zjsy.controller;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zjsystem.zjsy.dto.ZjSituation;
import com.zjsystem.zjsy.dto.ZjSubmitLog;
import com.zjsystem.zjsy.dto.ZjTask;
import com.zjsystem.zjsy.result.Result;
import com.zjsystem.zjsy.service.ZjSituationService;
import com.zjsystem.zjsy.service.ZjSubmitLogService;
import com.zjsystem.zjsy.service.ZjTaskService;
import com.zjsystem.zjsy.service.ZjZtService;
import com.zjsystem.zjsy.vo.TaskVO;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 战机任务表 前端控制器
 * </p>
 *
 * @author wyf
 * @since 2024-06-20
 */
@Slf4j
@RestController
@RequestMapping("/zjsy/zjTask")
public class ZjTaskController {
    @Resource
    private ZjTaskService taskService;
    @Resource
    private ZjSituationService situationService;
    @Resource
    private ZjSubmitLogService submitLogService;

    @ApiModelProperty("任务调控 任务展示")
    @GetMapping("/taskList")
    public Result taskList(@RequestParam("zjId") String zjId) {
        log.info("入参的战机id为：{}", zjId);
        List<TaskVO> result = new ArrayList<>();
        try {
            List<ZjTask> list = taskService.list(Wrappers.<ZjTask>lambdaQuery()
                    .eq(ZjTask::getZjid, zjId));
            if (CollectionUtils.isEmpty(list)){
                return Result.ok(result);
            }
            List<String> taskIds = list.stream().map(ZjTask::getId).collect(Collectors.toList());
            List<ZjSubmitLog> submitLogList = submitLogService.list(Wrappers.<ZjSubmitLog>lambdaQuery()
                    .in(ZjSubmitLog::getTaskId, taskIds));

                List<ZjSubmitLog> submitLogs = new ArrayList<>();
                list.forEach(task -> {
                    String taskId = task.getId();
                    TaskVO vo = new TaskVO();
                    BeanUtils.copyProperties(task, vo);
                    submitLogList.forEach(sub -> {
                        String subTaskId = sub.getTaskId();
                        if (StringUtils.isNotBlank(subTaskId) && subTaskId.equals(taskId)){
                            submitLogs.add(sub);
                        }
                    });
                    vo.setSubmitLogList(submitLogs);
                    result.add(vo);
                });
            return Result.ok(result);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail(e.getMessage());
        }
    }

    @ApiModelProperty("任务调控 战场态势展示")
    @GetMapping("/situationList")
    public Result situationList(@RequestParam("zjId") String zjId) {
        log.info("入参的战机id为：{}", zjId);
        try {
            List<ZjSituation> list = situationService.list(Wrappers.<ZjSituation>lambdaQuery()
                    .eq(ZjSituation::getZjId, zjId));
            return Result.ok(list);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail(e.getMessage());
        }
    }
}

